package service;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.SeekBar;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import activity.Medaiaplay;

/**
 * Created by dllo on 15/11/21.
 */
public class MediaService extends Service
       {

    public MediaPlayer mediaPlayer; // 媒体播放器

           @Override
           public void onCreate() {
               super.onCreate();
               mediaPlayer = new MediaPlayer();



               mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);// 设置媒体流类型
           }

           private Medaiaplay.Stub sb = new Medaiaplay.Stub() {


        @Override
        public void player() throws RemoteException {
            mediaPlayer.start();
        }

        @Override
        public void stop() throws RemoteException {
            if (mediaPlayer != null) {
                mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer = null;
            }
        }

        @Override
        public void pause() throws RemoteException {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
            }
            else {
                mediaPlayer.start();
            }
        }

        @Override
        public void playUrl(String url) throws RemoteException {
            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(url); // 设置数据源
                mediaPlayer.prepare(); // prepare自动播放


            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public long getDuration() throws RemoteException {
            return mediaPlayer.getDuration();//获取音频时常

        }

        @Override
        public int currentPositon() throws RemoteException {
            return mediaPlayer.getCurrentPosition();//返回当前播放的进度
        }

        @Override
        public void seekTo(int positon) throws RemoteException {
            mediaPlayer.seekTo(positon);//设置播放进度 media方法seekTo
        }
    };
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {

        return sb;
    }


}
